﻿using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Validation.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
using System;

namespace AsNum.Common.EntLib.Validation.Adapters {
    /// <summary>
    /// 只支持日期,不支持时间
    /// </summary>
    public class DateTimeRangeClientAdapter : BaseClientAdapter<DateTimeRangeValidatorData> {

        //TODO 这里的时间日期范围到哪个纬度?日期?包括时间?
        private static readonly string fmt = "yyyy-MM-dd HH:mm:ss";

        public DateTimeRangeClientAdapter(DateTimeRangeValidatorData data , Dictionary<string , Tuple<string , string>> map , bool isSub = false)
            : base(data, map , isSub) {
            if (this.ValidatorData.LowerBoundType != RangeBoundaryType.Ignore) {
                this.AddRule(RuleKeys.Min, string.Format("'{0}'", this.ValidatorData.LowerBound.ToString(fmt)));
                this.AddRule(RuleKeys.IncludeMin, (this.ValidatorData.LowerBoundType == RangeBoundaryType.Inclusive).JsBool());
            }

            if (this.ValidatorData.UpperBoundType != RangeBoundaryType.Ignore) {
                this.AddRule(RuleKeys.Max, string.Format("'{0}'", this.ValidatorData.LowerBound.ToString(fmt)));
                this.AddRule(RuleKeys.IncludeMax, (this.ValidatorData.UpperBoundType == RangeBoundaryType.Inclusive).JsBool());
            }
        }

        public override RuleTypes RuleType {
            get {
                return RuleTypes.DateRange;
            }
        }
    }
}
